package cn.ai.shoes.client.service.impl;

import cn.ai.shoes.client.entity.ComfyuiModel;
import cn.ai.shoes.client.service.FreemarkService;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import freemarker.template.Configuration;
import freemarker.template.Template;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.io.StringWriter;
import java.util.Map;

@Service
public class FreemarkServiceImpl implements FreemarkService {
    public static final String CONFIG = "config";
    @Resource
    private Configuration configuration;
    /**
     * 图像融合
     *
     * @param model
     * @return
     * @throws Exception
     */
    @Override
    public JSONObject renderImageTemp(ComfyuiModel model, String templateName) throws Exception {
        // 1. 获取模板
        Template template = configuration.getTemplate(templateName);
        //2. 替换模板中的变量
        StringWriter temp = new StringWriter();
        template.process(Map.of(CONFIG,model), temp);
        String jsonStr = temp.toString();
        return JSON.parseObject(jsonStr);
    }
}
